package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;
import java.util.Objects;


import cn.hutool.core.bean.BeanUtil;


import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.standard.dto.CdcLabOrderDto;
import com.dhcc.sds.standard.dto.CdcLabitemDto;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandLabitemMatchEntity;
import com.dhcc.sds.standard.service.ISdsStandLabitemMatchService;


/**
 * 标准化_检验项目配置
 * 
 * @ClassName: SdsStandLabitemMatchController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandlabitemmatch")
public class SdsStandLabitemMatchController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstandlabitemmatch/";

	@Autowired
	private ISdsStandLabitemMatchService sdsStandLabitemMatchService;

    @Autowired
    private ISdsStandDicService sdsStandDicService;
	

    /**
     * 跳转到标准化_检验项目配置列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("dicMap",sdsStandDicService.getDicMapBySource("检验生化"));
        return PREFIX + "sdsstandlabitemmatch.html";
    }
	
    /**
     * 跳转到新增标准化_检验项目配置
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsstandlabitemmatch_add.html";
    }
	
	/**
     * 跳转到修改标准化_检验项目配置
     */
    @RequestMapping("/to_update/{sdsStandLabitemMatchId}")
    public String toUpdate(@PathVariable Long sdsStandLabitemMatchId, Model model) {
        SdsStandLabitemMatchEntity sdsStandLabitemMatch = this.sdsStandLabitemMatchService.selectById(sdsStandLabitemMatchId);
        model.addAttribute("sdsStandLabitemMatch",sdsStandLabitemMatch);
        return PREFIX + "sdsstandlabitemmatch_edit.html";
    }

    /**
     * 获取标准化_检验项目配置列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params, @RequestParam String compare) {
		Page<SdsStandLabitemMatchEntity> page = new PageFactory<SdsStandLabitemMatchEntity>().defaultPage();
        List<SdsStandLabitemMatchEntity> result = this.sdsStandLabitemMatchService.page(page,params,compare);
        page.setRecords((List<SdsStandLabitemMatchEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增标准化_检验项目配置
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandLabitemMatchEntity sdsStandLabitemMatch) {
        if (CommonUtil.isOneEmpty(sdsStandLabitemMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandLabitemMatch.insert();
        return R.ok();
    }

    /**
     * 删除标准化_检验项目配置
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandLabitemMatchId) {
	
        this.sdsStandLabitemMatchService.deleteById(sdsStandLabitemMatchId);

        return R.ok();
    }

    /**
     * 修改标准化_检验项目配置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandLabitemMatchEntity sdsStandLabitemMatch) {
        if (CommonUtil.isOneEmpty(sdsStandLabitemMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandLabitemMatchEntity old = this.sdsStandLabitemMatchService.selectById(sdsStandLabitemMatch.getId());

        old.setDicsubId(sdsStandLabitemMatch.getDicsubId());
        old.setLabitemId(sdsStandLabitemMatch.getLabitemId());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 同步检验项目到标准化-检验项目配置表
     */
    @RequestMapping(value = "/sync")
    @ResponseBody
    public Object sync() {

        this.sdsStandLabitemMatchService.sync();

        return R.ok();
    }

    /**
     * 标准化-检验项目配置对照
     */
    @RequestMapping(value = "/compare")
    @ResponseBody
    public Object compare(@RequestParam Long sdsStandDicsubId, @RequestParam String labitemIds, @RequestParam Long sdsStandDicId, @RequestParam String laborderIds) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandLabitemMatchService.compare(sdsStandDicsubId, labitemIds, sdsStandDicId, laborderIds, username);

        return R.ok();
    }

    /**
     * 撤销标准化_医嘱对照
     */
    @RequestMapping(value = "/undo")
    @ResponseBody
    public Object undo(@RequestParam String labitemIds, @RequestParam Long sdsStandDicsubId, @RequestParam String laborderIds, @RequestParam Long sdsStandDicId) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandLabitemMatchService.undo(labitemIds, sdsStandDicsubId, laborderIds, sdsStandDicId, username);

        return R.ok();
    }

    /**
     * 自动匹配标准化-检验医嘱
     */
    @RequestMapping(value = "/autoCompareLaborder")
    @ResponseBody
    public Object autoCompareLaborder() {

        this.sdsStandLabitemMatchService.autoCompareLaborder();

        return R.ok();
    }

    /**
     * 自动匹配标准化-检验项目配置
     */
    @RequestMapping(value = "/autoCompareLabitem")
    @ResponseBody
    public Object autoCompareLabitem() {

        this.sdsStandLabitemMatchService.autoCompareLabitem();

        return R.ok();
    }


    /**
     * 获取检验项目表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listLabitem")
    @ResponseBody
    public Object listLabitem(@RequestParam Map<String, Object> params) {

        if (Objects.equals("match", params.get("compare"))){
            Page<SdsStandLabitemMatchEntity> page = new PageFactory<SdsStandLabitemMatchEntity>().defaultPage();
            List<CdcLabitemDto> result = this.sdsStandLabitemMatchService.pageMatchLabitem(page,params);

            Page<CdcLabitemDto> dtoPage = new PageFactory<CdcLabitemDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcLabItemEntity> page = new PageFactory<CdcLabItemEntity>().defaultPage();
            List<CdcLabitemDto> result = this.sdsStandLabitemMatchService.pageLabitem(page,params);

            Page<CdcLabitemDto> dtoPage = new PageFactory<CdcLabitemDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }

    }




    /**
     * 获取检验医嘱表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listLaborder")
    @ResponseBody
    public Object listLaborder(@RequestParam Map<String, Object> params) {
        Page<CdcLabOrderEntity> page = new PageFactory<CdcLabOrderEntity>().defaultPage();
        List<CdcLabOrderDto> result = this.sdsStandLabitemMatchService.pageLaborder(page,params);

        Page<CdcLabOrderDto> dtoPage = new PageFactory<CdcLabOrderDto>().defaultPage();
        BeanUtil.copyProperties(page,dtoPage);
        dtoPage.setRecords(result);
        return R.packForBT(dtoPage);
    }

    /**
     * 获取对照检验医嘱表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listMatchLaborder")
    @ResponseBody
    public Object listMatchLaborder(@RequestParam Map<String, Object> params) {

        if (Objects.equals("match", params.get("compare"))){
            Page<SdsStandLabitemMatchEntity> page = new PageFactory<SdsStandLabitemMatchEntity>().defaultPage();
            List<CdcLabOrderDto> result = this.sdsStandLabitemMatchService.pageMatchLaborder(page,params);

            Page<CdcLabOrderDto> dtoPage = new PageFactory<CdcLabOrderDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcLabOrderEntity> page = new PageFactory<CdcLabOrderEntity>().defaultPage();
            List<CdcLabOrderDto> result = this.sdsStandLabitemMatchService.pageLaborder(page,params);

            Page<CdcLabOrderDto> dtoPage = new PageFactory<CdcLabOrderDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }

    }

    /**
     * 对照的标准字典进行渲染
     */
    @RequestMapping(value = "/matchDicsub")
    @ResponseBody
    public Object matchDicsub() {

        List<Long> matchIds = this.sdsStandLabitemMatchService.selectMatchDicsub();

        return matchIds;
    }
	
}
